package org.come.xinyuan;

import cn.hutool.core.util.StrUtil;
import com.tool.role.SkillUtil;
import com.tool.tcpimg.UIUtils;
import org.apache.commons.lang.StringUtils;
import org.come.Frame.MsgJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.MsgJapnel;
import org.come.bean.PathPoint;
import org.come.bean.Skill;
import org.come.bean.TYCPointBean;
import org.come.bean.TYCTypeBean;
import org.come.entity.RoleSummoning;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;
import org.skill.panel.SkillSMSelectOptionJpanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

 

public class CopyOfSkillTYCPanel5 extends JPanel {
    private JLabel maxPointLab;
    private SkillTYCBtn6 exchangeBtn;
    private SkillTYCBtn6 washPointBtn;
    private SkillTYCBtn6 confirmBtn;
    private SkillTYCBtn6 yncsBtn;//一念成圣
    private SkillSMSelectOptionJpanel TYCselect;
    public int typeBtn;
    public MSkillTYC[] skillTYCs;
    private TYCRole tycRole;

    public CopyOfSkillTYCPanel5() {
        setBounds(0, 30, 901, 548);
        setOpaque(false);
        setLayout(null);
        exchangeBtn = new SkillTYCBtn6("skin/8100226/newbutton/B30.png", 1, UIUtils.COLOR_BTNTEXT, "兑换", UIUtils.TEXT_FONT, 1, this);
        exchangeBtn.setBounds(165, 407, 34, 17);
        add(exchangeBtn);
        washPointBtn = new SkillTYCBtn6("skin/8100226/newbutton/B30.png",  1, UIUtils.COLOR_BTNTEXT, "洗点", UIUtils.TEXT_FONT, 2, this);
        washPointBtn.setBounds(205, 407, 34, 17);
        add(washPointBtn);
        confirmBtn = new SkillTYCBtn6("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_WHITE2, "确认加点", UIUtils.TEXT_HY16, 4, this);
        confirmBtn.setBounds(430, 452, 100, 24);
        add(confirmBtn);
        yncsBtn = new SkillTYCBtn6("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_WHITE2, "成圣", UIUtils.TEXT_HY16, 3, this);
        yncsBtn.setBounds(480, 452, 100, 24);
        add(yncsBtn);
        maxPointLab = new JLabel();
        maxPointLab.setForeground(Color.white);
        maxPointLab.setBounds(195, 357, 60, 22);
        maxPointLab.setFont(UIUtils.TEXT_BOLD_FONT);
        add(maxPointLab);
        tycRole = new TYCRole();
        skillTYCs = new MSkillTYC[1];

        for (int i = 0; i < skillTYCs.length; ++i) {
            this.skillTYCs[i] = new MSkillTYC();
            this.skillTYCs[i].px = 130;
        }

        this.changeBtnPanel();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }

    public void addPoint() {
        for (int i = 0; i < skillTYCs.length; ++i) {
            if (skillTYCs[i].visible) {
                StringBuffer buffer = new StringBuffer();
                for (int j = 0; j < skillTYCs[i].lists.size(); ++j) {
                    XSkillTYC skillTYC = skillTYCs[i].lists.get(j);
                    if (skillTYC.tycPointBean.getNowPoint() >= 1) {
                        buffer.append(skillTYC.tycPointBean.getTableId().toString());
                        buffer.append("|");
                    }
                }
                String skill = "";
                if (StringUtils.isNotEmpty(buffer.toString())) {
                    skill = buffer.substring(0, buffer.length() - 1);
                }
                String save = Agreement.getAgreement().XinYuanAgreement("S&" + UserMessUntil.getChosePetMes().getSid() + "&" + skill);
                SendMessageUntil.toServer(save);
            }
        }

    }

    public void changeBtnPanel() {

        String xinyuan = UserMessUntil.getChosePetMes().getXinyuan();
        if (StrUtil.isNotBlank(xinyuan) && xinyuan.split("&").length > 1) {
            String[] data = xinyuan.split("&")[1].split("\\|");
            initTYC("心意", data, 0);
        } else {
            initTYC("心意", null, 0);
        }

    }

    public void initTYC(String seName, String[] data, int lvl) {
        tycRole.data = data;
        maxPointLab.setText("" + tycRole.getDian());
        String[] vs = SkillUtil.getSepciesS1(seName);
        if (tycRole.getDian() >= 12) {
            this.add(yncsBtn);
            confirmBtn.setBounds(380, 452, 100, 26);
        } else {
            this.remove(yncsBtn);
            confirmBtn.setBounds(430, 452, 100, 26);
        }
        if (vs != null) {
            for (int i = 0; i < vs.length; ++i) {
                skillTYCs[i].reset(vs[i], data, lvl);
            }
        }
    }


    public void changePoint(XSkillTYC xSkillTYC, boolean is) {
        if (is) {
            if (xSkillTYC.tycPointBean.getNowPoint() >= xSkillTYC.tycPointBean.getMaxPoint()) {
                return;
            }
        } else if (xSkillTYC.tycPointBean.getNowPoint() <= 0) {
            return;
        }

        MSkillTYC skillTYC = skillTYCs[xSkillTYC.px];
        if (skillTYC != null) {
            PathPoint point = skillTYC.getPoint();
            if (is) {
                if (tycRole.getDian() <= point.getX()) {
                    ZhuFrame.getZhuJpanel().addPrompt2("心意点不足!");
                    return;
                }

                if (!skillTYC.isCondition(xSkillTYC, point, tycRole.getLvl(), is)) {
                    return;
                }

                xSkillTYC.setPoint(xSkillTYC.tycPointBean.getNowPoint() + 1);
                skillTYC.refreshPanel(tycRole.getLvl());

                for (int i = 0; i < skillTYCs.length; ++i) {
                    if (this.skillTYCs[i].visible && this.skillTYCs[i] != skillTYC) {
                        this.skillTYCs[i].setShow(false);
                    }
                }

                maxPointLab.setText(String.valueOf(tycRole.getDian() - point.getX() - 1));
            } else {
                xSkillTYC.setPoint(xSkillTYC.tycPointBean.getNowPoint() - 1);
                PathPoint point2 = skillTYC.getPoint();

                int i;
                for (i = skillTYC.lists.size() - 1; i >= 0; --i) {
                    XSkillTYC xSkillTYC2 = skillTYC.lists.get(i);
                    if (xSkillTYC != xSkillTYC2 && xSkillTYC2.tycPointBean.getNowPoint() != 0 && !skillTYC.isCondition(xSkillTYC2, point2, tycRole.getLvl(), is)) {
                        xSkillTYC.setPoint(xSkillTYC.tycPointBean.getNowPoint() + 1);
                        return;
                    }
                }

                if (point2.getX() == 0) {
                    for (i = 0; i < this.skillTYCs.length; ++i) {
                        skillTYCs[i].reset(skillTYCs[i].value, (String[]) null, tycRole.getLvl());
                    }
                } else {
                    skillTYC.refreshPanel(tycRole.getLvl());
                }

                this.maxPointLab.setText(String.valueOf(tycRole.getDian() - point.getX() + 1));
            }

        }
    }

    public String getSkillMsg(Skill skill, XSkillTYC xSkillTYC) {
        TYCPointBean bean = xSkillTYC.tycPointBean;
        TYCTypeBean typeBean = bean.getTycTypeBean();
        int id = bean.getTableId();
        int row = bean.getRow();
        MSkillTYC skillTYC = skillTYCs[xSkillTYC.px];
        if (skillTYC != null && skill != null) {
            PathPoint point = skillTYC.getPoint();
            StringBuffer buffer = new StringBuffer();
            buffer.append("#r#cd3d3d3");
            String skilllevel = skill.getSkilllevel();
            skill.setSkilllevel("" + bean.getNowPoint());
            buffer.append(MsgJapnel.skillMsgchange(skill.getRemark(), skill));
            skill.setSkilllevel(skilllevel);
            if (typeBean == null) {
                return buffer.toString();
            } else {
                boolean is = true;
                Integer dian = typeBean.getLvl();
                if (dian != null && dian > this.tycRole.getLvl()) {
                    if (is) {
                        buffer.append("#r#r#cff0000");
                        is = false;
                    } else {
                        buffer.append("#r");
                    }

                    buffer.append("需要");
                    buffer.append(AnalysisString.lvl(dian));
                }

                dian = typeBean.getOneTotalPoint();
                if (dian != null && dian > point.getY()) {
                    if (is) {
                        buffer.append("#r#r#cff0000");
                        is = false;
                    } else {
                        buffer.append("#r");
                    }

                    buffer.append("需要第一境界#c00ff00");
                    buffer.append(dian);
                    buffer.append("#cff0000点");
                }

                dian = typeBean.getTotalPoint(bean.getNowPoint());
                if (dian != null && dian > point.getX()) {
                    if (is) {
                        buffer.append("#r#r#cff0000");
                        is = false;
                    } else {
                        buffer.append("#r");
                    }

                    buffer.append("需要总心意点数#c00ff00");
                    buffer.append(dian);
                    buffer.append("#cff0000点");
                }

                dian = typeBean.getQZID();
                if (dian != null) {
                    XSkillTYC skillTYC2 = skillTYC.getTyc(dian);
                    if (skillTYC2 != null && typeBean.getQZPoint() > skillTYC2.tycPointBean.getNowPoint()) {
                        Skill skill2 = UserMessUntil.getSkillId(dian.toString());
                        if (skill2 != null) {
                            if (is) {
                                buffer.append("#r#r#cff0000");
                                is = false;
                            } else {
                                buffer.append("#r");
                            }

                            buffer.append("需要点亮#c00ff00");
                            buffer.append(skill2.getSkillname());
                            buffer.append("#cff0000");
                        }
                    }
                }

                List<Integer> list = typeBean.getExclusiveSkills();
                if (list != null) {
                    for (int i = 0; i < list.size(); ++i) {
                        dian = (int) JmSum.MZ((long) (Integer) list.get(i));
                        if (!dian.equals(bean.getTableId())) {
                            Skill skill2 = UserMessUntil.getSkillId(dian.toString());
                            if (skill2 != null) {
                                if (is) {
                                    buffer.append("#r#r#cff0000");
                                    is = false;
                                } else {
                                    buffer.append("#r");
                                }

                                buffer.append("与#c00ff00");
                                buffer.append(skill2.getSkillname());
                                buffer.append("#cff0000技能互斥");
                            }
                        }
                    }
                }

                return buffer.toString();
            }
        } else {
            return "好像出问题了?联系管理员提交BUG";
        }
    }

    public SkillSMSelectOptionJpanel getTYCselect() {
        return this.TYCselect;
    }

    public void setTYCselect(SkillSMSelectOptionJpanel tYCselect) {
        this.TYCselect = tYCselect;
    }

    class MSkillTYC {
        private List<XSkillTYC> lists;
        private String value;
        private int px;
        private Integer p;
        public boolean visible;

        MSkillTYC() {
        }

        public void reset(String value, String[] data, int lvl) {
            this.setShow(true);
            int i;
            if (this.value != null && this.value.equals(value)) {
                if (this.lists == null) {
                    this.lists = new ArrayList();
                }
                for (i = this.lists.size() - 1; i >= 0; --i) {
                    this.lists.get(i).setPoint(0);
                }
            } else {
                this.value = value;
//                String[] values = UserMessUntil.getLX(value).split("\\|");
                String[] values = "11#25001|12#25002|13#25003#Z1|14#25004#Z1|15#25005#Z2|16#25006#Z2|22#25007#Q25003-1#Z3|23#25008#Q25004-1#Z3|24#25009#Z4|33#25010#Z4|34#25011#Q25007-1#Z6|43#25012#Q25008-1#Z6|44#25013#Q25011-1#Z7|51#25014#Q25012-1#Z7|52#25015#Q25011-1#Z10|53#25016#Q25012-1#Z10".split("\\|");
                if (this.lists == null) {
                    this.lists = new ArrayList();
                }
                int p = 0;
                int size = this.lists.size();
                for (int ix = 0; ix < values.length; ++ix) {
                    String[] vs = values[ix].split("#");
                    TYCPointBean bean = new TYCPointBean();
                    int zhi = Integer.parseInt(vs[0]);
                    bean.setRow(zhi / 10);
                    bean.setRank(zhi % 10);
                    bean.setTableId(Integer.parseInt(vs[1]));
                    Skill skill = UserMessUntil.getSkillId(vs[1]);
                    if (skill != null) {
                        bean.setMaxPoint(1);
                    } else {
                        bean.setMaxPoint(5);
                    }
                    if (vs.length > 2) {
                        TYCTypeBean tycTypeBean = new TYCTypeBean();
                        int j = 2;
                        while (true) {
                            if (j >= vs.length) {
                                bean.setTycTypeBean(tycTypeBean);
                                break;
                            }
                            String L = vs[j].substring(1);
                            if (vs[j].startsWith("D")) {
                                tycTypeBean.setOneTotalPoint(Integer.parseInt(L));
                            } else {
                                String[] ls = L.split("-");
                                if (vs[j].startsWith("Q")) {
                                    tycTypeBean.setQZID(Integer.parseInt(ls[0]));
                                    tycTypeBean.setQZPoint(Integer.parseInt(ls[1]));
                                } else {
                                    int k;
                                    if (vs[j].startsWith("Z")) {
                                        for (k = 0; k < ls.length; ++k) {
                                            tycTypeBean.setTotalPoint(Integer.parseInt(ls[k]));
                                        }
                                    } else if (vs[j].startsWith("C")) {
                                        for (k = 0; k < ls.length; ++k) {
                                            tycTypeBean.setExclusiveSkill(Integer.parseInt(ls[k]));
                                        }
                                    } else if (vs[j].startsWith("L")) {
                                        tycTypeBean.setLvl(AnalysisString.lvldirection(("4".equals(ls[0]) ? "飞升" : ls[0] + "转") + ls[1]));
                                    }
                                }
                            }
                            ++j;
                        }
                    }
                    if (p < size) {
                        this.lists.get(p).reset(bean, this.px);
                        ++p;
                    } else {
                        XSkillTYC skillTYCx = new XSkillTYC(bean, p);
                        ++p;
                        this.lists.add(skillTYCx);
                        add(skillTYCx);
                    }
                }
                for (int ix = size - 1; ix >= p; --ix) {
                    remove(this.lists.remove(ix));
                }
            }
            if (data != null) {
                for (i = 0; i < data.length; ++i) {
                    XSkillTYC skillTYC = this.getTyc(Integer.parseInt(data[i]));
                    if (skillTYC != null) {
                        skillTYC.setPoint(1);
                    }
                }
                PathPoint point = this.getPoint();
                maxPointLab.setText(String.valueOf(tycRole.getDian() - point.getX()));
            }
            this.refreshPanel(lvl);
        }

        public void refreshPanel(int lvl) {
            if (this.lists != null) {
                PathPoint point = this.getPoint();

                for (int i = this.lists.size() - 1; i >= 0; --i) {
                    XSkillTYC skillTYC = this.lists.get(i);
                    boolean is = this.isCondition(skillTYC, point, lvl, true);
                    if (is) {
                        skillTYC.setIcon1(null);
                    } else {
                        skillTYC.setIcon1(CutButtonImage.getImage("inkImg/old/background/S295.png", 22, 22));
                    }

                    if (skillTYC.tycPointBean.getNowPoint() == 1) {
                        skillTYC.setIcon2(CutButtonImage.getImage("inkImg/old/background/S294.png", -1, -1));
                    } else {
                        skillTYC.setIcon2(CutButtonImage.getImage("inkImg/old/background/S296.png", -1, -1));
                    }
                }
            }

        }

        public boolean isCondition(XSkillTYC skillTYC, PathPoint point, int lvl, boolean type) {
            TYCPointBean bean = skillTYC.tycPointBean;
            TYCTypeBean typeBean = bean.getTycTypeBean();
            if (typeBean == null) {
                return true;
            } else {
                Integer dian = typeBean.getQZID();
                if (dian != null) {
                    label67:
                    {
                        if (type) {
                            if (!type) {
                                break label67;
                            }
                        } else if (skillTYC.tycPointBean.getNowPoint() == 0) {
                            break label67;
                        }

                        XSkillTYC skillTYC2 = this.getTyc(dian);
                        if (skillTYC2 == null) {
                            return false;
                        }

                        if (typeBean.getQZPoint() > skillTYC2.tycPointBean.getNowPoint()) {
                            return false;
                        }
                    }
                }

                dian = typeBean.getLvl();
                if (dian != null && dian > lvl) {
                    return false;
                } else {
                    dian = typeBean.getOneTotalPoint();
                    if (dian != null && dian > point.getY()) {
                        return false;
                    } else {
                        dian = typeBean.getTotalPoint(bean.getNowPoint());
                        if (dian != null && dian > point.getX()) {
                            return false;
                        } else {
                            List<Integer> list = typeBean.getExclusiveSkills();
                            if (list != null) {
                                for (int i = 0; i < list.size(); ++i) {
                                    dian = (int) JmSum.MZ((long) (Integer) list.get(i));
                                    if (!dian.equals(bean.getTableId())) {
                                        XSkillTYC skillTYC2x = this.getTyc(dian);
                                        if (skillTYC2x != null && skillTYC2x.tycPointBean.getNowPoint() > 0) {
                                            return false;
                                        }
                                    }
                                }
                            }

                            return true;
                        }
                    }
                }
            }
        }

        public XSkillTYC getTyc(int id) {
            for (int i = this.lists.size() - 1; i >= 0; --i) {
                if (this.lists.get(i).tycPointBean.getTableId() == id) {
                    return this.lists.get(i);
                }
            }

            return null;
        }

        public PathPoint getPoint() {
            PathPoint point = new PathPoint();

            for (int i = this.lists.size() - 1; i >= 0; --i) {
                TYCPointBean bean = (this.lists.get(i)).tycPointBean;
                point.setX(point.getX() + bean.getNowPoint());
//                if (bean.getRow() <= 2) {
//                    point.setY(point.getY() + bean.getNowPoint());
//                }
            }

            return point;
        }

        public void setShow(boolean flag) {
            this.visible = flag;
            if (this.lists != null) {
                for (int i = this.lists.size() - 1; i >= 0; --i) {
                    (this.lists.get(i)).setVisible(this.visible);
                }
            }

        }
    }

    public TYCRole getTycRole() {
        return tycRole;
    }

    public void setTycRole(TYCRole tycRole) {
        this.tycRole = tycRole;
    }

    class TYCRole {
        private int lvl = 999;
        private int dian = 60;
        private String[] data;

        public TYCRole() {
            this.setLvl(0);
            this.setDian(0);
        }

        public Integer getLvl() {
            return 0;
        }

        public void setLvl(Integer lvl) {
            this.lvl = (int) JmSum.ZM((long) lvl);
        }

        public Integer getDian() {
            RoleSummoning pet = UserMessUntil.getChosePetMes();
            return pet != null && StringUtils.isNotBlank(pet.getXinyuan()) ? Integer.parseInt(pet.getXinyuan().split("&")[0]) : 0;
        }

        public void setDian(Integer dian) {
            this.dian = (int) JmSum.ZM((long) dian);
        }

    }

    class XSkillTYC extends JComponent implements MouseListener {
        private TYCPointBean tycPointBean;
        private ImageIcon icon1;
        private ImageIcon icon2;
        private ImageIcon icon3;
        private String value;
        private int py;
        private int px;
        int[] zuobiaox = new int[]{467, 391, 319, 608, 547, 384, 233, 691, 542, 462, 204, 719, 270, 658, 226, 695, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        int[] zuobiaoy = new int[]{325, 288, 387, 376, 282, 192, 294, 293, 190, 146, 163, 160, 136, 130, 78, 65, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

        public XSkillTYC(TYCPointBean tycPointBean, int x) {
            this.addMouseListener(this);
            this.reset(tycPointBean, x);
            this.px = x / 176;
        }

        public void reset(TYCPointBean tycPointBean, int x) {
            this.tycPointBean = tycPointBean;
            this.icon1 = null;
            this.icon2 = CutButtonImage.getImage("inkImg/old/background/S296.png", 22, 22);
            this.setPoint(0);
            this.py = 0;
            this.setBounds(this.zuobiaox[x], this.zuobiaoy[x], 22, 22);
        }

        public void setPoint(int point) {
            this.value = this.tycPointBean.setPoint(point);
            if (this.value.length() == 1) {
                this.icon3 = CutButtonImage.getImage("inkImg/old/background/S40.png", 22, 22);
            } else {
                this.icon3 = CutButtonImage.getImage("inkImg/old/background/S39.png", 22, 22);
            }

        }

        public void setIcon1(ImageIcon icon1) {
            this.icon1 = icon1;
        }

        public void setIcon2(ImageIcon icon2) {
            this.icon2 = icon2;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (this.icon2 != null) {
                g.drawImage(this.icon2.getImage(), this.py, this.py, 22, 22, this);
            }

            if (this.icon3 != null) {
                g.drawImage(this.icon3.getImage(), 0, 21, this);
            }

            if (this.value != null) {
                g.setColor(Color.yellow);
                g.setFont(UIUtils.TEXT_FONT);
                g.drawString(this.value, 3, 33);
            }

            if (this.icon1 != null) {
                g.drawImage(this.icon1.getImage(), 0, 0, this);
            }

        }

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == 1) {
                changePoint(this, true);
            } else {
                e.getButton();
            }

        }

        @Override
        public void mousePressed(MouseEvent e) {
            ++this.py;
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            --this.py;
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            Skill skill = UserMessUntil.getSkillId("" + this.tycPointBean.getTableId());
            MsgJframe.getJframe().getJapnel().TYC(skill != null ? "    " + skill.getSkillname() : "", getSkillMsg(skill, this));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            FormsManagement.HideForm(46);
        }
    }
}